//
//  OrderFooter.m
//  jingdongfang
//
//  Created by 郝志宇 on 16/8/11.
//  Copyright © 2016年 ZhiYu Hao. All rights reserved.
//

#import "OrderFooter.h"
#import "QueRenViewController.h"
#import "IGTakeOrderApi.h"
#import "FaHuoViewController.h"
#import "IGAddOrderApi.h"
#import "BuyCllowerViewController.h"
#import "IGGoodsAddOrderApi.h"
@interface OrderFooter ()
{
    UIView *view;
    UILabel *msg;
    UIButton *btn;
    NSInteger type;
}
@property(nonatomic,strong)IGTakeOrderApi *takeOrderApi;
@end

@implementation OrderFooter

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        [self _initViews];
    }
    return self;
}

- (void)_initViews
{
    self.contentView.backgroundColor =[UIColor c2Color];
    view = [[UIView alloc] initWithFrame:CGRectZero];
    view.backgroundColor = [UIColor whiteColor];
    
    msg = [[UILabel alloc] initWithFrame:CGRectZero];
    
    btn=[UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    if (_info)
    {
        view.frame = CGRectMake(0, 0, self.width, self.height-10);
        [self addSubview:view];

//        msg.text = _isMyBuy ? ([self.info.artPoint floatValue] > 0 ? [NSString stringWithFormat:@"艺点已抵扣: ¥%@ 应付: ¥%@", self.info.artPoint, self.info.actuallyPay] : [NSString stringWithFormat:@"合计: ¥%@", self.info.total.length>0?self.info.total:@"0"]) :[NSString stringWithFormat:@"合计: ¥%@", self.info.total.length>0?self.info.total:@"0"];
        msg.text = _isMyBuy ? ([self.info.artPoint floatValue] > 0 ? [NSString stringWithFormat:@"%@: ¥%@ %@: ¥%@", NSLocalizedString(@"Art Point Deducted",nil),self.info.artPoint, NSLocalizedString(@"Payment",nil),self.info.actuallyPay] : [NSString stringWithFormat:@"%@: ¥%@", NSLocalizedString(@"Total",nil),self.info.total.length>0?self.info.total:@"0"]) :[NSString stringWithFormat:@"%@: ¥%@", NSLocalizedString(@"Total",nil),self.info.total.length>0?self.info.total:@"0"];
        msg.font = [UIFont font14];
        [self addSubview:msg];
        [msg sizeToFit];

        [btn.layer setCornerRadius:15.f];
        [btn.layer setBorderColor:[UIColor c1Color].CGColor];
        [btn.layer setBorderWidth:1.0];
        btn.titleLabel.font=[UIFont font14];
        [btn setTitleColor:[UIColor appColor] forState:UIControlStateNormal];
        btn.contentEdgeInsets = UIEdgeInsetsMake(6,12,6,12);
        if(self.isMyBuy)
        {
            if ([self.info.status integerValue] == 0)
            {  //未支付
//                [btn setTitle:@"去付款" forState:UIControlStateNormal];
                [btn setTitle:NSLocalizedString(@"Go to the payment", nil) forState:UIControlStateNormal];
                [btn sizeToFit];
                btn.hidden = NO;
            }
            else if ([self.info.status integerValue]==2)
            {
//                [btn setTitle:@"确认收货" forState:UIControlStateNormal];
                [btn setTitle:NSLocalizedString(@"Confirm Delivery", nil) forState:UIControlStateNormal];

                [btn sizeToFit];
                btn.hidden = NO;
            }
            else
            {
                btn.width=0;
                btn.hidden=YES;
            }
        }
        else if (self.isMyBuy==NO)
        {
            if ([self.info.status integerValue]==1)
            {
//                [btn setTitle:@"发货" forState:UIControlStateNormal];
                [btn setTitle:NSLocalizedString(@"Shipped", nil) forState:UIControlStateNormal];

                [btn sizeToFit];
                btn.hidden=NO;
            }
            else
            {
                btn.width=0;
                btn.hidden=YES;
            }
        }
        [self addSubview:btn];

        btn.right=self.width-15;
        btn.centerY=(self.height-10)/2;
        
        msg.right = self.width-15-btn.width-15;
        msg.centerY = (self.height-10)/2;
    }
}

-(void)btnClick:(UIButton *)btn
{
    if(self.isMyBuy==YES)
    {
        if ([_info.status integerValue]==0)
        {  //去付款
            if([self.info.type integerValue]==1){//限量收藏去付款
            IGAddOrderApi *api = [IGAddOrderApi new];
            api.worksId = _info.pId;
            api.orderId = _info.oId;
            [api startWithDecodeClass:[IGAddOrderResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
                if (responseObject)
                {
                    IGAddOrderResp *resp = responseObject;
                    IGAddOrderModel *model = resp.data;
                    BuyCllowerViewController *vc = [[BuyCllowerViewController alloc] init];
                    vc.type = 2;
                    vc.model = model;
                    [UIViewController ig_pushViewController:vc animated:YES];
                }
            }];
            }
            if ([self.info.type integerValue]!=1) {//真品购买去付款
            IGGoodsAddOrderApi *orderApi = [IGGoodsAddOrderApi new];
            orderApi.goodsId = _info.pId;
            orderApi.orderId=_info.oId;
            [orderApi startWithDecodeClass:[IGGoodsAddOrderResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
                if (responseObject)
                {
                    IGGoodsAddOrderResp * resp = responseObject;
                    IGGoodsAddOrderModel *model = resp.data;
                    if ([model.method isEqualToString:@"toOrder"])
                    {
                        QueRenViewController * vc = [[QueRenViewController alloc] init];
                        vc.model = model;
                        vc.type = 2;
                        [UIViewController ig_pushViewController:vc animated:YES];
                    }
                }
            }];
            }
        }
        else if ([_info.status integerValue]==2)
        {  //去确认收货
            self.takeOrderApi=[IGTakeOrderApi new];
            self.takeOrderApi.oid=_info.oId;
            self.takeOrderApi.type = @"3";
            [self.takeOrderApi startWithDecodeClass:[IGTakeOrderResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
                if (responseObject) {
                    btn.hidden=YES;
                    [self reloadInputViews];
                    self.accept();
                    //需要给orderHeader返回已完成参数;
                }
            }];
        }
    }
    else if (self.isMyBuy==NO)
    {
        if ([_info.status integerValue]==1)
        {  //发货
            FaHuoViewController * vc = [[FaHuoViewController alloc]initWithNibName:@"FaHuoViewController" bundle:nil];
            vc.o_id =_info.oId;
            [UIViewController ig_pushViewController:vc animated:YES];
        }
    }
    
}


@end
